﻿using System.Collections.Generic;
using Box2DX.Collision;
using Box2DX.Dynamics;

namespace Expression.Samples.Physics
{
	public class SimpleContactListener : ContactListener
	{
		List<ContactPoint> contactPoints = new List<ContactPoint>();

		public override void Add(ContactPoint point)
		{
			base.Add(point);

			this.contactPoints.Add(point);
		}

		public override void Remove(ContactPoint point)
		{
			base.Remove(point);
		}

		public void ProcessContacts() {
			List<ContactPoint> points = new List<ContactPoint>(this.contactPoints);
			this.contactPoints.Clear();

			foreach (ContactPoint contactPoint in points) {
				Entity a = contactPoint.Shape1.UserData as Entity;
				Entity b = contactPoint.Shape2.UserData as Entity;

				if (a != null)
					a.OnCollide(contactPoint);
				if (b != null)
					b.OnCollide(contactPoint);
			}
		}
	}
}
